<?php

/**
 * MySql save tables
 *
 * PHP version 5
 *
 * Copyright © 2013 François Cero
 *
 * This file is part of Galette (http://galette.tuxfamily.org).
 *
 * Plugin MySqlsave is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Plugins
 * @package   MySqlsave
 *
 * @author    François Cero <cerol@laposte point net>
 * @copyright 2013 François Cero
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
 * @version   0.1
 * @link      http://galette.tuxfamily.org
 * @since     Available since 0.7
 */

define('GALETTE_BASE_PATH', '../../');
require_once GALETTE_BASE_PATH . 'includes/galette.inc.php';
if ( !$login->isLogged() ) {
    header('location: ' . GALETTE_BASE_PATH . 'index.php');
    die();
}
if (!$login->isSuperAdmin()) {
    header('location: ' . GALETTE_BASE_PATH . 'index.php');
    die();
}
require_once '_config.inc.php';


$CSVdataFile= $_FILES['csvfile']['tmp_name'];
//echo "CSVdataFile".$CSVdataFile  ;
 //   die();
 
 
 
 

//$dossier = 'upload/';
//$fichier = basename($_FILES['avatar']['name']);



//$taille_maxiIMPORT = 400000; this parameter is now defined in _config.inc.php
$taille = filesize($_FILES['csvfile']['tmp_name']);
$extensions = array('.csv', '.CSV', '.Csv', '.cSV');
$extension = strrchr($_FILES['csvfile']['name'], '.'); 
//Début des vérifications de sécurité...
if(!in_array($extension, $extensions)) //Si l'extension n'est pas dans le tableau
{
     $erreur = _T("CSV SAVE.EXT");
}
if($taille>$taille_maxiIMPORT)
{
     $erreur = _T("CSV SAVE.SIZE");
}
if(!isset($erreur)) //S'il n'y a pas d'erreur, on ecrit dans la base
{
// Setup for the import
//$pathToCSVimport = "./";				// Full system directory path to the CSVimporter class file (see the  _config.inc.php file for galette)
//$CSVdataFile = "./testgalette.csv";			// Full system path to the csv file you want to import
$dbHost = HOST_DB;					// MySQL database host
$dbUsername = USER_DB;					// MySQL database username
$dbPassword = PWD_DB;					// MySQL database password
$dbName = NAME_DB;						// MySQL database to use
$dbImportTable = "galette_adherents";	// Name of the MySQL table to import the CSV data into

// Make database connection before importing CSV data
$conn = mysql_connect($dbHost, $dbUsername, $dbPassword);
$db = mysql_select_db($dbName, $conn);
// $fieldslist is now defined in _config.inc.php
//$fieldslist=array("nom_adh", "prenom_adh", "sexe_adh", "ddn_adh", "adresse_adh", "adresse2_adh", "cp_adh", "ville_adh", "tel_adh", "email_adh" , "prof_adh" , "info_public_adh", "login_adh", "mdp_adh", "activite_adh", "date_crea_adh", );
$CSV = new CSVimporter($CSVdataFile);						// Instantiate new import object
$CSV->setFieldNames($fieldslist);	// Set the field names for the CSV data
$CSV->queryInto($dbImportTable);							// Start the import process

// ---------------------------------------------------------------------------------------------------------
mysql_close($conn);

//create a field names lists to inform user if errors in csv content 

foreach($fieldslist AS $field => $value)
		{
		   $valuefield.=$value."\n";
		}

}
/**
 * Le traitement est terminé, on affiche le template
 */
$tpl->assign('page_title', _T("CSV SAVE.SAVEDTITLE"));
//Set the path to the current plugin's templates,
//but backup main Galette's template path before
$orig_template_path = $tpl->template_dir;
$tpl->template_dir = 'templates/' . $preferences->pref_theme;
$tpl->assign('fields_list', $valuefield);
$tpl->assign('error_EXT', $erreur);
$tpl->assign('error_CSV', $errorCSV);
$tpl->assign('error_SQL', $errorSQL);
$tpl->assign('end_import', _T("CSV SAVE.MESS1").$dbName. _T("CSV SAVE.MESS2"));


$content = $tpl->fetch('csvimport.tpl', SAVE_SMARTY_PREFIX);
$tpl->assign('content', $content);
//Set path to main Galette's template
$tpl->template_dir = $orig_template_path;
$tpl->display('page.tpl', SAVE_SMARTY_PREFIX);

/*else
{
     echo $erreur;
}
*/
 
 
 
 
 
 
 
 
 
 



?>